package com.zf.shuashua.manager;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;

import com.meis.base.mei.calendar.CalendarEvent;
import com.meis.base.mei.calendar.CalendarProviderManager;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zf.shuashua.api.Store;
import java.util.Calendar;

/**
 * 日程提醒
 */
public class CalendarManager {

    private static CalendarManager instance;

    private CalendarManager() {

    }

    public static CalendarManager getInstance() {
        if (null == instance) {
            synchronized (CalendarManager.class) {
                if (null == instance) {
                    instance = new CalendarManager();
                }
            }
        }
        return instance;
    }

    public boolean isCalendarReminded() {
        return Store.getInstance().isCalendarReminded();
    }

    public void setCalendarReminded(boolean isRemind) {
        Store.getInstance().setCalendarReminded(isRemind);
    }

    /**
     * 设置国庆日历提醒
     */
    public void startCalendarEvent(Context context) {
        String format = "FREQ=DAILY;INTERVAL=1;UNTIL=20201008T000000Z";
        CalendarEvent calendarEvent = new CalendarEvent(
                "【长豆短视频】升国旗！天天领奖，连玩3天赚10万！",
                "【长豆短视频】升国旗！天天领奖，连玩3天赚10万！",
                "【长豆短视频】",
                1601523000,
                1601523000,
                0, format, ""

        );
        // 添加事件
        int result = CalendarProviderManager.addCalendarEvent(context, calendarEvent);
        if (result == 0) {
            Toast.makeText(context, "已帮你开启日程提醒", Toast.LENGTH_SHORT).show();
        } else if (result == -1) {
            Toast.makeText(context, "开启失败", Toast.LENGTH_SHORT).show();
        } else if (result == -2) {
            Toast.makeText(context, "没有日历权限", Toast.LENGTH_SHORT).show();
        } else {

        }
    }


    // 开启提醒规则
    @SuppressLint("CheckResult")
    public void startCalendarRemind(FragmentActivity activity) {
        // 请求权限
        new RxPermissions(activity).request(new String[]{
                Manifest.permission.READ_CALENDAR,
                Manifest.permission.WRITE_CALENDAR})
                .subscribe(aBoolean -> {
                    if (aBoolean) {
                        startWithdrawalCalendarEvent(activity);
                        Store.getInstance().setCalendarReminded(true);
                    } else {
                        Toast.makeText(activity, "请允许日历权限", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        intent.setData(Uri.parse("package:" + activity.getPackageName()));
                        if (!isIntentAvailable(activity, intent)) return;
                        activity.startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                    }
                });
    }


    /**
     * 开启提现日历事件
     */
    public void startWithdrawalCalendarEvent(Context context) {
        int year = Calendar.getInstance().get(Calendar.YEAR);
        int month = Calendar.getInstance().get(Calendar.MONTH);
        int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);

        Calendar start = Calendar.getInstance();
        start.set(year, month, day, 11, 30);

        if (month == 1 && day >= 28) {
            day = 28;
        }

        month += 1;

        String monthStr = "";

        if (month < 10) {
            monthStr = "0" + month;
        } else {
            monthStr = month + "";
        }

        String dayStr = "";
        if (day < 10) {
            dayStr = "0" + dayStr;
        } else {
            dayStr = day + "";
        }

        String format = "FREQ=DAILY;INTERVAL=1;UNTIL=" + (year + 1) + monthStr + dayStr + "T000000Z";

        CalendarEvent calendarEvent = new CalendarEvent(
                "【长豆短视频】快来提现~每日提现可赚更多现金！！",
                "【长豆短视频】快来提现~每日提现可赚更多现金！！",
                "",
                start.getTime().getTime(),
                start.getTime().getTime(),
                0, format
        );

        // 添加事件
        int result = CalendarProviderManager.addCalendarEvent(context, calendarEvent);
        if (result == 0) {
            Toast.makeText(context, "已帮你开启日程提醒", Toast.LENGTH_SHORT).show();
        } else if (result == -1) {
            Toast.makeText(context, "开启失败", Toast.LENGTH_SHORT).show();
        } else if (result == -2) {
            Toast.makeText(context, "没有日历权限", Toast.LENGTH_SHORT).show();
        }
    }


    /**
     * @param context
     * @param intent
     * @return
     */
    private boolean isIntentAvailable(Context context, final Intent intent) {
        return context
                .getPackageManager()
                .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
                .size() > 0;
    }

}
